package cn.itsource.service.impl;


import cn.hutool.core.util.IdUtil;
import cn.itsource.constants.Constants;
import cn.itsource.exception.GlobalException;
import cn.itsource.mapper.CustomerMapper;
import cn.itsource.pojo.api.AddLogin;
import cn.itsource.pojo.app.dto.AppRegisterDto;
import cn.itsource.pojo.domain.Customer;
import cn.itsource.pojo.domain.CustomerWallet;
import cn.itsource.pojo.domain.CustomerWalletFlow;
import cn.itsource.result.R;
import cn.itsource.romate.api.LoginApi;
import cn.itsource.service.ICustomerService;
import cn.itsource.service.ICustomerSummaryService;
import cn.itsource.service.ICustomerWalletFlowService;
import cn.itsource.service.ICustomerWalletService;
import cn.itsource.template.WechatTemplate;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 客户 服务实现类
 * </p>
 *
 * @author beishan
 * @since 2024-03-25
 */
@Service
@Transactional
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {
    @Autowired
    private WechatTemplate wechatTemplate;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    private ICustomerSummaryService customerSummaryService;

    @Autowired
    private ICustomerWalletService customerWalletService;

    @Autowired
    private ICustomerWalletFlowService customerWalletFlowService;

   @Autowired
   private LoginApi loginApi;

    /**
     * 客户注册
     * @param appRegisterDto ： 前端传入的授权码
     * @return
     */
    @Override

    public Object register(AppRegisterDto appRegisterDto) {
        String openid = wechatTemplate.getOpenid(appRegisterDto.getWxCode());
        String phone = wechatTemplate.getPhone(appRegisterDto.getPhoneCode());

        //通过openid 去查询数据库
        Customer one = super.getOne(new LambdaQueryWrapper<Customer>().eq(Customer::getOpenId, openid));
        //判断获取的对象 是否为空
        if (one != null) {
            //不为空 抛出异常
            throw new GlobalException("该微信已注册");
        }
        //为空  添加到数据库
        //使用雪花算法 生成 id
        long id = IdUtil.createSnowflake(1, 1).nextId();
         addCustomer(openid, id, phone);

        //将id 添加到 ** 表 维护表
        customerSummaryService.createCustomerSummary(id);

       customerWalletService.createCustomerWallet(id);

       customerWalletFlowService.createCustomerWalletFlow(id);

        //远程维护login 表

        addCustomerToLogin(id,openid,phone);

        return null;
    }

    public void addCustomer(String openId, long id, String number) {
        Customer customer = new Customer();
        customer.setId(id);
        customer.setSex(0);
        customer.setPhone(number);
        customer.setEmail("");
        customer.setCreateTime(new Date());
        customer.setVersion(0);
        customer.setDeleted(false);
        customer.setOpenId(openId);
        customer.setBitState(0L);
        customer.setName("");
        customer.setLevel("");

        super.save(customer);

    }

    public void addCustomerToLogin(Long id,String openid,String phone){
        AddLogin addLogin = new AddLogin();
        addLogin.setId(id);
        addLogin.setOpenId(openid);
        addLogin.setPhone(phone);
        addLogin.setUsername(phone);
        addLogin.setType(Constants.Login.TYPE_CUSTOMER);
        addLogin.setAvatar("https://img0.baidu.com/it/u=342808525,3109725284&fm=253&fmt=auto&app=138&f=JPEG?w=426&h=398");
        R r = loginApi.addLogin(addLogin);
        if (!r.isSuccess()) {
            throw new GlobalException("注册失败");
        }
    }


}
